#!/usr/bin/env bash

# install fpm
#
# On OSX/macOS:
# brew install gnu-tar
#
# On Red Hat systems (Fedora 22 or older, CentOS, etc):
# yum install ruby-devel gcc make rpm-build
#
# On Fedora 23 or newer:
# dnf install ruby-devel gcc make rpm-build
#
# On Debian-derived systems (Debian, Ubuntu, etc):
# apt-get install ruby ruby-dev rubygems gcc make
#
# Installing FPM:
# gem install --no-ri --no-rdoc fpm
#

# check
if [ $# -lt 1 ]; then
    echo "Usage: ./scripts/makepkg [rpm|deb|osxpkg|pacman]"
    exit
fi
xmakeroot=`pwd`
artifactsdir=$xmakeroot/artifacts
if [ ! -d $artifactsdir ]; then
    mkdir $artifactsdir
fi

# target
target=$1

# version
version=`cat ./core/xmake.lua | grep -E "^set_version" | grep -oE "[0-9]*\.[0-9]*\.[0-9]*"`

# arch
arch=i386
if [ `getconf LONG_BIT` == "64" ]; then
    arch=x86_64
fi

# platform
plat=linux
if [ `uname` == "Darwin" ]; then
    plat=macosx
fi

# build first
./configure
make

# make dep package using fpm
fpm -s dir -t $target -n xmake -v $version -m "<waruqi@gmail.com>" --description "A cross-platform build utility based on Lua" --url "xmake.io" --license Apache \
        xmake=/usr/local/share/ \
        build/xmake=/usr/local/bin/xmake \
        scripts/xrepo.sh=/usr/local/bin/xrepo
if [ "$target" == "osxpkg" ]; then
    mv "xmake-$version.pkg" "$artifactsdir/xmake.pkg"
elif [ "$target" == "deb" ]; then
    mv "xmake_${version}_amd64.deb" "$artifactsdir/xmake.deb"
fi


